'use strict';
const bcrypt = require('bcryptjs')

module.exports = {
  up: async (queryInterface, Sequelize) => {
    /**
     * Add altering commands here.
     *
     * Example:
     * await queryInterface.createTable('users', { id: Sequelize.INTEGER });
     */
    const { INTEGER, DATE, STRING } = Sequelize;
    await queryInterface.createTable('admins', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      nickname: {
        type: Sequelize.STRING(64),
        allowNull: false,
        comment: '管理员昵称'
      },
      email: {
        type: Sequelize.STRING(128),
        unique: true,
        allowNull: false,
        comment: '管理员邮箱'
      },
      password: {
        type: Sequelize.STRING,
        set(val) {
          // 加密
          const salt = bcrypt.genSaltSync(10);
          // 生成加密密码
          const psw = bcrypt.hashSync(val, salt);
          this.setDataValue("password", psw);
        },
        allowNull: false,
        comment: '管理员密码'
      },
      created_at: DATE,
      updated_at: DATE,
    });
  },

  down: async (queryInterface, Sequelize) => {
    /**
     * Add reverting commands here.
     *
     * Example:
     * await queryInterface.dropTable('users');
     */
    await queryInterface.dropTable('admins');
  }
};
